package com.yellowpage.nsp.action;
  
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.ReadLog;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.nsp.data.NspLogSum;
import com.yellowpage.nsp.service.NspLogSumService;
import com.yellowpage.nsp.service.NspLoggerService;

public class NspLogSumJFreeChartAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	public String RESULT_TELLDAY = "tellDay";
	
	public String RESULT_TELLMONTH = "tellMonth";
	public String RESULT_SHOW = "show";
	
	private Configuration configuration;
	private NspLoggerService nspLoggerService;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<NspLogSum> nspLogSumList = new ArrayList<NspLogSum>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private NspLogSum nspLogSum;
	private NspLogSumService nspLogSumService;
	private ReadLog readlog;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	private String JFreeChartshow;
	private String JFreeChartshow2;
		
		private String query_requestType;
		private String query_requestTime;
		private String query_requestTime_end;
		
		private Map requestTypeMap = new HashMap();
		
	public void prepare() {
		super.prepare();
		requestTypeMap=nspLoggerService.getrequestTypeMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspLogSum = nspLogSumService.getNspLogSum(Long.parseLong(selectedId));
		}
	}
	
	public String nspLogerSumTellDayJFreeChart(){
		
		return RESULT_TELLDAY;
	}

	
	public String nspLogerSumTellDayJFreeChartQuery() throws ServletException{
		 
		Map queryMap = new HashMap();	
	    queryMap.put("requestType", query_requestType);
	    queryMap.put("requestTimeStart", query_requestTime);
	    queryMap.put("requestTimeEnd", query_requestTime_end);
	    
	    resultList = nspLogSumService.queryNspLogSumTellDay(queryMap);
	    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	    DefaultCategoryDataset datasetday = new DefaultCategoryDataset();
	    
	    
	    for (Map map : resultList) {
	    	
	    	dataset.addValue((Number) map.get("REQCOUNT"),"'"+map.get("COMPANY")+"'", "'"+map.get("LOG_DATE")+"'");  
	    	Number i=null;
	    	if(map.get("INCREMENTIMSITEL")==""||map.get("INCREMENTIMSITEL")==null){
	    		i=(Number) map.get("ALLIMSITEL");
	    	}else{
	    		i=(Number) map.get("INCREMENTIMSITEL");
	    	}
	    	
	    	datasetday.addValue(i,"'"+map.get("COMPANY")+"'", "'"+map.get("LOG_DATE")+"'");  
	    }
	    
	    HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType( "text/html;charset=GBK "); 
		String path = request.getSession().getServletContext().getRealPath("/nspjfreechart");
		//path = path.replace("\\","//");
		FileOutputStream fos=null;
		FileOutputStream fosday=null;
		//String folder="/nspjfreechart";
		 File file = new File(path);  
		        if(!file.exists()){  
		        	file.setExecutable(true); 
		        	file.setReadable(true);
		        	file.setWritable(true);
		            file.mkdir();  
		        }  

	      //请求数折线图
	     JFreeChart chart = ChartFactory.createLineChart(  
	           "请求数",                    // 标题  
	           "时间",                      // 横坐标  
	           "总数",                     // 纵坐标  
	           dataset,                    // 数据  
	           PlotOrientation.VERTICAL,   // 竖直图表  
	           true,                       // 是否显示legend  
	           false,                      // 是否显示tooltip  
	           false                       // 是否使用url链接  
	       );  
	  
	     
	     
	   
	        CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
	        //背景
	        categoryplot.setBackgroundPaint(Color.white);
	        //网格
	        categoryplot.setDomainGridlinesVisible(true);  
	      //设置背景网格线颜色
	        categoryplot.setDomainGridlinePaint(Color.BLACK); 
	        
	        categoryplot.setRangeGridlinePaint(Color.GRAY);
	      //没有数据说明
	        categoryplot.setNoDataMessage("没有数据");
	      
			  
			 //标题
			TextTitle textTitle = chart.getTitle();
			textTitle.setFont(new Font("黑体", Font.PLAIN, 20));     
			//横轴
			CategoryAxis domainAxis = categoryplot.getDomainAxis(); 
			domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
			domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
			domainAxis.setLabelFont(new Font("黑体", Font.PLAIN, 22));  
			//纵轴
			NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
			numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
			numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 22));  
			
			chart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 12));
			//折线
			 LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
			 lineandshaperenderer.setSeriesPaint(0, Color.black);   
			 //数据点
			 lineandshaperenderer.setBaseShapesVisible(true); 
				// 显示折点数据
/*		    lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		     lineandshaperenderer.setBaseItemLabelsVisible(true);*/
		     
			 //日增量折线图
		     JFreeChart chartday = ChartFactory.createLineChart(  
			           "日增量",                    // 标题  
			           "时间",                      // 横坐标  
			           "IMSI,TEL有效",                     // 纵坐标  
			           datasetday,                    // 数据  
			           PlotOrientation.VERTICAL,   // 竖直图表  
			           true,                       // 是否显示legend  
			           false,                      // 是否显示tooltip  
			           false                       // 是否使用url链接  
			       );  
			 CategoryPlot categoryplotday = (CategoryPlot) chartday.getPlot();
		        //背景
			 categoryplotday.setBackgroundPaint(Color.white);
		        //网格
			 categoryplotday.setDomainGridlinesVisible(true);  
		      //设置背景网格线颜色
			 categoryplotday.setDomainGridlinePaint(Color.BLACK); 
		        
			 categoryplotday.setRangeGridlinePaint(Color.GRAY);
		      //没有数据说明
			 categoryplotday.setNoDataMessage("没有数据");
		      
				  
				 //标题
				TextTitle textTitleday = chartday.getTitle();
				textTitleday.setFont(new Font("黑体", Font.PLAIN, 20));     
				//横轴
				CategoryAxis domainAxisday = categoryplotday.getDomainAxis(); 
				domainAxisday.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
				domainAxisday.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
				domainAxisday.setLabelFont(new Font("黑体", Font.PLAIN, 22));  
				//纵轴
				NumberAxis numberaxisday = (NumberAxis) categoryplotday.getRangeAxis();
				numberaxisday.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
				numberaxisday.setLabelFont(new Font("黑体", Font.PLAIN, 22));  
				
				chartday.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 12));
				//折线
				 LineAndShapeRenderer lineandshaperendererday = (LineAndShapeRenderer) categoryplotday.getRenderer();
				 lineandshaperendererday.setSeriesPaint(0, Color.black);   
				 //数据点
				 lineandshaperendererday.setBaseShapesVisible(true); 
			 
		     
			    SimpleDateFormat formatter = new SimpleDateFormat("ss");
			    Date currentTime = new Date();
			    String dateString = formatter.format(currentTime);
	      try {  
	          fos = new FileOutputStream(path  + "/nspjfreechart"+dateString+".png");
	          ChartUtilities.writeChartAsPNG(fos, chart, 1000, 550);  	
	          fosday = new FileOutputStream(path  + "/nspjfreechartday"+dateString+".png");
	          ChartUtilities.writeChartAsPNG(fosday, chartday, 1000, 550);
		      
	      } catch (FileNotFoundException e) {  
	       e.printStackTrace();  
	      } catch (IOException e) {  
	       e.printStackTrace();  
	      } finally {  
	          try {  
	           if(fos != null){  
	            fos.close();  
	            
	           }  
	       } catch (IOException e) {  
	        e.printStackTrace();  
	       }  
	      }  
	     
	  	String path2 = request.getContextPath();
	      JFreeChartshow=path2+"/nspjfreechart/nspjfreechart"+dateString+".png";
	      JFreeChartshow2=path2+"/nspjfreechart/nspjfreechartday"+dateString+".png";
	    return RESULT_TELLDAY;
		
	}



	/**
	 * getters and setters
	 * 
	 * @return
	 */

	public NspLoggerService getNspLoggerService() {
		return nspLoggerService;
	}



	public void setNspLoggerService(NspLoggerService nspLoggerService) {
		this.nspLoggerService = nspLoggerService;
	}



	public List<NspLogSum> getNspLogSumList() {
		return nspLogSumList;
	}



	public void setNspLogSumList(List<NspLogSum> nspLogSumList) {
		this.nspLogSumList = nspLogSumList;
	}



	public List<Map> getResultList() {
		return resultList;
	}



	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}



	public NspLogSum getNspLogSum() {
		return nspLogSum;
	}



	public void setNspLogSum(NspLogSum nspLogSum) {
		this.nspLogSum = nspLogSum;
	}



	public NspLogSumService getNspLogSumService() {
		return nspLogSumService;
	}



	public void setNspLogSumService(NspLogSumService nspLogSumService) {
		this.nspLogSumService = nspLogSumService;
	}



	public ReadLog getReadlog() {
		return readlog;
	}



	public void setReadlog(ReadLog readlog) {
		this.readlog = readlog;
	}



	public String getSelectedId() {
		return selectedId;
	}



	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}



	public String[] getSelectedIds() {
		return selectedIds;
	}



	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}



	public String getMessage() {
		return message;
	}



	public void setMessage(String message) {
		this.message = message;
	}



	public String getQuery_requestType() {
		return query_requestType;
	}



	public void setQuery_requestType(String query_requestType) {
		this.query_requestType = query_requestType;
	}



	public String getQuery_requestTime() {
		return query_requestTime;
	}



	public void setQuery_requestTime(String query_requestTime) {
		this.query_requestTime = query_requestTime;
	}



	public String getQuery_requestTime_end() {
		return query_requestTime_end;
	}



	public void setQuery_requestTime_end(String query_requestTime_end) {
		this.query_requestTime_end = query_requestTime_end;
	}



	public Map getRequestTypeMap() {
		return requestTypeMap;
	}



	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public String getJFreeChartshow() {
		return JFreeChartshow;
	}

	public void setJFreeChartshow(String jFreeChartshow) {
		JFreeChartshow = jFreeChartshow;
	}

	public String getJFreeChartshow2() {
		return JFreeChartshow2;
	}

	public void setJFreeChartshow2(String jFreeChartshow2) {
		JFreeChartshow2 = jFreeChartshow2;
	}




	   
	
	
	

	 
	
}
